package com.wiscamp.ninechapters.problems.infrastructure.mappers.sets;

import com.wiscamp.ninechapters.problems.infrastructure.po.sets.RegroupRuleView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;

/**
 * 组题规则仓储对象
 *
 * @author yifeng
 */
@Repository
public interface RegroupRuleViewMapper extends JpaRepository<RegroupRuleView, Long>, JpaSpecificationExecutor<RegroupRuleView> {
    /**
     * 通过代码获取考试
     *
     * @param ruleCode
     * @param organizationId
     * @return
     */
    @Query(value = "select e from RegroupRuleView e where e.organizationId = :organizationId and e.ruleCode = :ruleCode and e.deleted = false")
    RegroupRuleView findByRuleCode(String ruleCode, long organizationId);

    /**
     * 通过名称获取考试列表
     *
     * @param ruleName
     * @param organizationId
     * @return
     */
    @Query(value = "select e from RegroupRuleView e where e.organizationId = :organizationId and e.ruleName = :ruleName and e.deleted = false")
    Collection<RegroupRuleView> findListByRuleName(String ruleName, long organizationId);


    @Query(value = "select e from RegroupRuleView e where e.organizationId = :organizationId and e.ruleCode like :ruleCode")
    Collection<RegroupRuleView> findListByRuleCode(String ruleCode, long organizationId);


    /**
     * 通过机构编号获取考试列表
     *
     * @param organizationId
     * @return
     */
    @Query(value = "select e from RegroupRuleView e where e.organizationId = :organizationId and e.deleted = false")
    Collection<RegroupRuleView> findListByOrganization(long organizationId);

    /**
     * 通过用户编号获取组题规则列表
     *
     * @param creatorId
     * @return
     */
    @Query(value = "select e from RegroupRuleView e where e.creatorId = :creatorId order by e.ruleId desc")
    Collection<RegroupRuleView> findListByCreator(long creatorId);

    @Query(value = "select e from RegroupRuleView e where e.ruleName like :namePrefix and e.enabled = true")
    Collection<RegroupRuleView> findListBySpecialPractice(String namePrefix);

    /**
     * 分页获取考试列表
     *
     * @param spec
     * @param pageable
     * @return
     */
    Page<RegroupRuleView> findAll(Specification<RegroupRuleView> spec, Pageable pageable);
}